//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//

package ar.com.ford.it.webengine.metric;

import java.util.GregorianCalendar;
import ar.com.ford.it.webengine.settings.AppSettings;

public class MetricManager {
	
	private static final ThreadLocal threadMetric = new ThreadLocal();
	private static boolean isActivate = AppSettings.getInstance().isUseMetrics();
	
	public static void startMetric(String txName, String userId) {
		if (isActivate) {
			Metric metric = new Metric();
			metric.setId(MetricsControllerFactory.getMetricsController().generateId());
			metric.setStart(new GregorianCalendar());
			metric.setTxName(txName);
			metric.setUser(userId);
		
			threadMetric.set(metric);
		}
	}
	
	public static void logMetric(String className, String methodName, String msg) {
		if (isActivate) {
			Metric metric = (Metric)threadMetric.get();
			if (metric != null) {
				MetricStep metricStep = new MetricStep();
				metricStep.setTime(new GregorianCalendar());
				metricStep.setClassName(className);
				metricStep.setMethodName(methodName);
				metricStep.setMsg(msg);
				metric.addStep(metricStep);
			}
		}
	}
	
	public static void endMetric() {
		if (isActivate) {
			Metric metric = (Metric)threadMetric.get();
			if (metric != null) {
				threadMetric.set(null);
				MetricsControllerFactory.getMetricsController().registerMetric(metric);
			}
		}
	}
	
}
